package com.mall.controller;

import com.github.pagehelper.PageInfo;
import com.mall.bean.SystemLogs;
import com.mall.model.ResponseBean;
import com.mall.service.SystemLogsService;
import com.mall.util.DateUtils;
import com.mall.vo.LogVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @PageName com.mall.controller
 * @Description 操作日志控制层
 * @Author 何全裕
 * @Create 2017-09-11 21:27
 * @Version 1.0
 **/
@Controller
@RequestMapping("/log")
public class SystemLogsController {

    @Autowired
    private SystemLogsService systemLogsService;

    @RequestMapping("/toLogs")
    public ModelAndView toLogs(){
        ModelAndView view = new ModelAndView("/system_log/SystemLog");
        return view;
    }

    @ResponseBody
    @RequestMapping("getLogsList")
    public ResponseBean getLogsList(SystemLogs logs, @RequestParam(required=false,defaultValue = "1")int page,
                                    @RequestParam(required=false,defaultValue = "10")int limit){
        PageInfo pageInfo = systemLogsService.getLogsListByPage(logs, page, limit);
        List<SystemLogs> logsList = pageInfo.getList();
        long total = pageInfo.getTotal();
        List<LogVo> list = new ArrayList<LogVo>();
        for(SystemLogs systemlogs : logsList){
            LogVo log = new LogVo();
            try {
                BeanUtils.copyProperties(log, systemlogs);
            } catch (Exception e) {
                e.printStackTrace();
            }
            String date = DateUtils.dateToString(systemlogs.getDateline());
            log.setDatelineVo(date);
            log.setAvailableVo(systemlogs.getAvailable().equals("1")?"成功":"失败");
            list.add(log);
        }
        ResponseBean bean = new ResponseBean(0,"",(int)total,list);
        return bean;
    }
}
